package com.itcj.dmp.utils

import ch.hsr.geohash.GeoHash
import com.maxmind.geoip.{Location, LookupService}
import org.lionsoul.ip2region.{DbConfig, DbSearcher}

object IpUtils {
  def convertIp2IpRegion(ip: String): IpRegion = {
    // 1. 创建IP转省市的对象
    val dbSearcher = new DbSearcher(new DbConfig(), "dataset/ip2region.db")
    // 2.创建IP转经纬度对象
    val lookupService= new LookupService("dataset/GeoLiteCity.dat")
    // 查询IP为省市区信息
    val regionArr: Array[String] = dbSearcher.btreeSearch(ip).getRegion.split("\\|")
    // 获取经纬度
    val location: Location = lookupService.getLocation(ip)
    val long: Double = location.longitude.toDouble
    val lat: Double = location.latitude.toDouble
    val geohash: String = GeoHash.geoHashStringWithCharacterPrecision(lat, long, 8)

    // 构造样例类
    IpRegion(ip, long, lat, regionArr(2), regionArr(3), geohash)
  }


  def main(args: Array[String]): Unit = {
    val region = convertIp2IpRegion("100.254.5.64")
    println(region)
  }
}


